package com.yuncheng.spcyApi.flow;

import cn.hutool.core.collection.CollectionUtil;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.spcyApi.vo.flow.SpcyJobXqVo;
import com.yuncheng.vo.GetKsKz;
import com.yuncheng.vo.CurrentUserVo;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.busService.BusService;
import com.yuncheng.spcyApi.constant.flow.RckpConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;

import com.yuncheng.spcyApi.utils.StringPatternUtils;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.flow.AgentVo;
import com.yuncheng.spcyApi.vo.spcy.SpcyReturnVo;
import com.yuncheng.spcyApi.vo.sqsx.SqsxZbrVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 日常考评流程
 * @author hua
 */
@Component(value = "jcyRckpFlowService")
public class JcyRckpFlowService {
    private static final Logger log = LoggerFactory.getLogger(JcyRckpFlowService.class);

    @Resource
    @Lazy
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    @Lazy
    private IYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private ISysUserFgldService sysUserFgldService;

    @Resource
    @Lazy
    private BusService busService;

    @Resource
    @Lazy
    private ISpcySqsxJobService spcySqsxJobService;

    @Resource
    @Lazy
    private ISysFlowNodeService sysFlowNodeService;

    @Resource
    @Lazy
    private FlowNodeService flowNodeService;

    /**
     * 处理流程
     * @param flowVo 流程处理参数
     * @param jobId 流程任务id
     * @param pid 流程业务id
     * @param submitType 0: 保存；1: 提交
     * @param opinion 办理意见
     * @return
     */
    public Result handleFlow(FlowVo flowVo, String jobId, String pid, String submitType, String opinion){
        return this.handleFlow(flowVo,jobId, null, pid, submitType, opinion);
    }

    /**
     * 处理流程
     * @param nowJob 当前流程任务
     * @return
     */
    public Result handleFlow(FlowVo flowVo, String jobId, SpcySqsxJob nowJob, String pid, String submitType, String opinion){
        if (StringUtils.isBlank(jobId)){
            throw new BusinessException("流程参数为空！");
        }

        // 当前流程任务
        if (nowJob == null) {
            nowJob = spcySqsxJobService.getById(jobId);

            if (nowJob == null){
                return Result.error("没有相匹配的流程！");
            }
        }

        pid = nowJob.getFpid();
        if (StringUtils.isBlank(pid)){
            throw new BusinessException("业务参数为空！");
        }

        // 提交按钮
        if (submitType.equals(RckpConstant.SUBMIT_TYPE_SUBMIT)) {
            if (nowJob == null) {
                return Result.error("当前流程任务不存在");
            }
        }
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return Result.error("登录已失效，请重新登录");
        }

        if (StringUtils.isBlank(nowJob.getFhjmc())){
            throw new BusinessException("流程环节名称为空！");
        }

        if (nowJob.getFhjmc().equals(SpcyFlowConstant.RCKP_HJMC_JBRSH)) {
            SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(pid);
            if (sqsx != null){
                sqsx.setFrckpdshblr(currentUser.getName());
                sqsx.setFrckpdshblrid(currentUser.getId());
                spcySqsxBaiscService.updateById(sqsx);
            }

            // 流程环节处理
            Result httpResult = handleNodeName(flowVo, submitType, nowJob, pid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }

        }else if (nowJob.getFhjmc().equals(SpcyFlowConstant.RCKP_HJMC_KSFH)) {
            SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(pid);
            if (sqsx != null){
                sqsx.setFrckpdfhblr(currentUser.getName());
                sqsx.setFrckpdfhblrid(currentUser.getId());
                spcySqsxBaiscService.updateById(sqsx);
            }

            Result httpResult = handleNodeName(flowVo, submitType, nowJob, pid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }
        }

        if (nowJob.getFhjmc().equals(SpcyFlowConstant.RCKP_HJMC_END)) {
            SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(pid);
            if (sqsx != null){
                if (StringUtils.isNotBlank(sqsx.getFywflcode())){
                    String dept = SpcyConstant.getYwclNameByCode(sqsx.getFywflcode());
                    Map fgldMap = this.getKsFgldByDeptName(dept);

                    sqsx.setFrckpdspblr(fgldMap.get("dbr").toString());
                    sqsx.setFrckpdspblrid(fgldMap.get("dbrId").toString());
                    spcySqsxBaiscService.updateById(sqsx);
                }
            }

            // 计算日常考评结果
            List<SpcySqsxXpjl> xpjlList = busService.getTyXpXpjlList(pid, SpcyConstant.FLOW_TYPE_CY);
            String frckpjg = "";
            if (CollectionUtils.isNotEmpty(xpjlList)) {
                for (SpcySqsxXpjl xpjl : xpjlList) {
                    if (StringUtils.isNotBlank(xpjl.getFzhpj())) {
                        frckpjg += xpjl.getFjcyxm() + ":" + xpjl.getFzhpj() + "; ";
                    }else {
                        frckpjg += xpjl.getFjcyxm() + ": 未填写。";
                    }
                }
            }
            if (sqsx != null){
                sqsx.setFrckpjg(frckpjg);
                sqsx.setFrckpsj(new Date());
                spcySqsxBaiscService.updateById(sqsx);
            }
        }

        return Result.ok("操作成功");
    }

    /**
     * 流程环节处理
     * @param pid 业务id
     * @param flowVo 需要修改的字段内容
     * @param job 流程任务job
     * @param opinion 办理意见
     * @param submitType 提交、保存类型
     * @return true(),false(提示信息)
     */
    public Result handleNodeName(FlowVo flowVo,String submitType, SpcySqsxJob job, String pid, String opinion){
        HashMap<String, Object> map = flowVo.getMap();
        String smgflag = flowVo.getSmgflag(); // 短信发送标志
        String smsContent = flowVo.getSmsContent(); // 发送短信内容

        Result res = null;

        // 保存按钮
        if (submitType.equals(SpcyFlowConstant.SUBMIT_TYPE_SAVE)){

        }

        // 提交按钮
        if (submitType.equals(SpcyFlowConstant.SUBMIT_TYPE_SUBMIT)){

            if (job == null){
                throw new BusinessException("当前流程任务不存在！");
            }

            if (StringUtils.isBlank(job.getFhjmc())){
                throw new BusinessException("当前流程任务环节名称丢失！");
            }

            // 流程任务id
            String jobid = job.getId();

            // 提交流程任务,待办人是否为空，指定待办人
            if (StringUtils.isBlank(flowVo.getDbrid())) {
                // 按审查模式-提交-流程任务
                res = this.passJob(pid, jobid, job, opinion, flowVo.getNextNode(),flowVo.getNextNodeId(), null);
            }else {
                // 按审查模式-提交-流程任务
                res = this.passJob(pid, jobid, job, opinion, flowVo.getNextNode(),flowVo.getNextNodeId(),flowVo.getDbrid());
            }

            if (!res.isSuccess()){
                return res;
            }else {
                /**
                 * 接收返回的流程任务，如果返回的流程任务id不相等，发送短信
                 * 发送下环节短信
                 */
                if (StringUtils.isNotBlank(smgflag) && (smgflag.equals("是") || smgflag.equals("true"))){
                    busService.sendSmsToNextJob(jobid,smsContent);
                }
            }
        }

        return Result.ok("提交成功");
    }

    /**
     * 初始化流程
     * @param pid 业务id
     * @param currentUser 当前登陆用户
     * @return
     */
//    @Async(value = "asyncNormalTask")
    public Result initJob(String pid, CurrentUserVo currentUser){
        return this.initJob(pid,currentUser,false);
    }

    /**
     * 初始化流程
     * @param pid 业务id
     * @param currentUser 当前登陆用户
     * @param isSendSms 是否发送短信
     * @return
     */
    public Result initJob(String pid, CurrentUserVo currentUser, boolean isSendSms){
        String lcmbCode = SpcyFlowConstant.KP_RCKH_FLOW_CODE;

        //获取流程模板的第一个环节
        SysFlowTemple code = sysFlowNodeService.getCode(lcmbCode);

        List<SysFlowNode> node = sysFlowNodeService.getFirstNode(code.getId());

        List<SpcySqsxJob> jobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFpid, pid)
                .eq(SpcySqsxJob::getFnodeid, node.get(0).getId()));
        if (CollectionUtils.isNotEmpty(jobList)){
            log.error("当前申请事项日常考评流程已经初始化了!{}", pid);
            return Result.error("当前申请事项环节["+node.get(0).getFjdmc()+"】已经初始化了!");
        }

        Result result = flowNodeService.createJob(lcmbCode,pid,"",node.get(0),null,null,currentUser);
        if (!result.isSuccess()){
            log.error("申请事项【{}】，初始化日常考评失败，错误信息：{}",pid, result.getMessage());
            return result;
        }else {
            if (isSendSms) {
                SpcySqsxJob job = (SpcySqsxJob) result.getResult();
                if (job != null) {
                    String nextNodeSmsContent = busService.getNextNodeSmsContent(job, null);
                    busService.sendSmsToNodeDbr(job.getFdbrid(), job.getFlcmc() + "-" + job.getFhjmc(), nextNodeSmsContent);
                }
            }
        }

        return result;
    }

    /**
     * 提交流程
     * @param pid 业务id
     * @param jobid 流程任务id
     * @param job 当前流程任务
     * @param opinion 办理意见
     * @param nextNodeName 下环节结点名称
     * @param nextNodeId 下环节结点id
     * @param nextNodeDbrIds 下环节待办人
     * @return
     */
    private Result passJob(String pid,String jobid,SpcySqsxJob job, String opinion,String nextNodeName,String nextNodeId,String nextNodeDbrIds){
        if (job == null){
            job = spcySqsxJobService.getById(jobid);
        }
        String jobNodeName = job.getFhjmc(); // 当前环节名称

        Result httpResult = null;

        // 是否为最终结点
        if (StringUtils.isNotBlank(jobNodeName) && jobNodeName.equals(SpcyFlowConstant.RCKP_HJMC_END)){
            //办理掉当前环节
            httpResult = flowNodeService.doJob(job, opinion);
        }else {
            httpResult = flowNodeService.passJob(SpcyFlowConstant.KP_RCKH_FLOW_CODE, pid, jobid, opinion, nextNodeId, nextNodeDbrIds);
        }

        if (!httpResult.isSuccess()){
            return httpResult;
        }

        return Result.ok("success");
    }

    /**
     * 退回流程
     */
    public void getReturnUpperFlowJob(SpcyReturnVo spcyReturnVo){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            throw new BusinessException("登录已失效，请重新登录！");
        }

        if (StringUtils.isBlank(spcyReturnVo.getJobid())){
            throw new BusinessException("流程任务参数为空！");
        }

        if (StringUtils.isBlank(spcyReturnVo.getOpinion())){
            throw new BusinessException("请填写退回意见！");
        }

        SpcySqsxJob currentJob = spcySqsxJobService.getById(spcyReturnVo.getJobid());
        if (currentJob == null){
            throw new BusinessException("退回失败！当前流程任务不存在！");
        }

        SpcySqsxJob previousJob = getCurrentJobPrevious(spcyReturnVo.getJobid(), currentJob);
        if (previousJob == null){
            throw new BusinessException("退回失败！选中的退回环节不存在！");
        }

        // 退回功能
        Result result = flowNodeService.returnJob(spcyReturnVo.getJobid(), currentJob, spcyReturnVo.getShjid(), previousJob, spcyReturnVo.getOpinion(), currentUser);
        if (result.isSuccess()){

            if (StringUtils.isNotBlank(spcyReturnVo.getSmgflag()) && (spcyReturnVo.getSmgflag().equals("是") || spcyReturnVo.getSmgflag().equals("true"))) {
                SpcySqsxJob newPreviousJob = (SpcySqsxJob) result.getResult();
                // 发送退回短信
                String nextNodeSmsContent = busService.getNextNodeSmsContent(newPreviousJob, null);
                busService.sendNextNodeSmsContent(newPreviousJob, nextNodeSmsContent);
            }
        }
    }

    /**
     * 查询审评查验流程当前环节-上一环节
     * @param jobid 流程任务id
     * @return
     */
    private SpcySqsxJob getCurrentJobPrevious(String jobid, SpcySqsxJob nowJob){
        if (StringUtils.isBlank(jobid)){
            throw new BusinessException("流程任务参数为空！");
        }
        SpcySqsxJob previousJob = null;

        if (nowJob == null) {
            nowJob = spcySqsxJobService.getById(jobid);
            if (nowJob == null) {
                throw new BusinessException("当前流程任务不存在！");
            }
        }

        if (StringUtils.isBlank(nowJob.getFnodeid())){
            throw new BusinessException("当前流程任务结点配置不存在！");
        }

        List<SysFlowNode> previousNodeList = sysFlowNodeService.getPreviousNode(nowJob.getFnodeid());
        if (CollectionUtil.isEmpty(previousNodeList)){
            return previousJob;
        }

        LambdaQueryWrapper<SpcySqsxJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxJob::getFpid, nowJob.getFpid());
        queryWrapper.orderByAsc(SpcySqsxJob::getFfssj);

        if (previousNodeList.size() == 1) {
            queryWrapper.eq(SpcySqsxJob::getFnodeid, previousNodeList.get(0).getId());
        }else {
            List<String> preIdsList = previousNodeList.stream().map(SysFlowNode::getId).collect(Collectors.toList());
            queryWrapper.in(SpcySqsxJob::getFnodeid, preIdsList);
        }

        // 上环节结点id-办理过的流程任务
        List<SpcySqsxJob> previousJobList = spcySqsxJobService.list(queryWrapper);

        for (int i=0; i<previousJobList.size(); i++){
            previousJob = previousJobList.get(0);
        }

        return previousJob;
    }

    /**
     * 获取当前日常考评相关信息
     * @param jobid 流程任务id
     */
    public Map getRckpxxByJobId(String jobid){
        Map map = new HashMap();

        // 当前流转记录
        SpcySqsxJob job = spcySqsxJobService.getById(jobid);
        if (job == null){
            log.error("当前流程任务id为【{}】的流转记录不存在",jobid);
            return map;
        }
        String sqsxid = job.getFpid();
        if (org.apache.commons.lang3.StringUtils.isBlank(sqsxid)){
            log.error("当前流程任务id为【{}】的流转记录中的申请事项id为空",jobid);
            return map;
        }

        // 申请事项基本信息
        SpcySqsxBasic sqsxBasic = spcySqsxBaiscService.getById(sqsxid);

        // 申请事项的选派记录-查验-同意选派
        List<SpcySqsxXpjl> cyxpjlList = busService.getTyXpXpjlList(sqsxid,SpcyConstant.FLOW_TYPE_CY);
        List<SpcySqsxXpjl> hcyXpjlList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(cyxpjlList)){
            hcyXpjlList = cyxpjlList.stream().filter(
                    x -> StringUtils.isNotBlank(x.getFssk()) && !x.getFssk().contains(SpcyConstant.HCY_SSK_YB))
                    .collect(Collectors.toList());
        }

        map.put("sqsx",sqsxBasic); // 申请事项基本信息
        map.put("job",job); // 当前流转记录
        map.put("cyxpjlList",hcyXpjlList); // 申请事项-选派记录-查验

        return map;
    }

    /**
     * 获取当前流程任务相关信息
     * @param jobid 流程任务id
     */
    public Map getJobConcernByJobId(String jobid){
        return getJobConcernByJobId(jobid, null);
    }

    /**
     * 获取当前流程任务相关信息
     * @param jobid 流程任务id
     */
    public Map getJobConcernByJobId(String jobid, SpcySqsxJob job){
        Map map = new HashMap();

        // 当前流转记录
        if (job == null) {
            job = spcySqsxJobService.getById(jobid);
            if (job == null) {
                log.error("当前流程任务id为【{}】的流转记录不存在", jobid);
                throw new BusinessException("流程任务不存在！");
            }
        }

        String sqsxid = job.getFpid();
        if (org.apache.commons.lang3.StringUtils.isBlank(sqsxid)){
            log.error("当前流程任务id为【{}】的流转记录中的申请事项id为空",jobid);
            throw new BusinessException("业务参数为空!");
        }

        // 申请事项基本信息
        SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);

        // 流程任务流传记录
        /*LambdaQueryWrapper<SpcySqsxJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxJob::getFpid, sqsxid);
        queryWrapper.like(SpcySqsxJob::getFlcmc, SpcyFlowConstant.RCKP_LCMC);
        queryWrapper.orderByAsc(SpcySqsxJob::getFfssj);
        queryWrapper.orderByAsc(SpcySqsxJob::getCreateTime);
        List<SpcySqsxJob> jobList = spcySqsxJobService.list(queryWrapper);*/

        map.put("sqsx", sqsx); // 申请事项基本信息
        map.put("job",job); // 当前流转记录
        map.put("jobHistoryList", spcySqsxJobService.selectHistoryListByFpidAndFlcmc(sqsxid, SpcyFlowConstant.RCKP_LCMC)); // 流程任务流传记录

        // 0: 不是主办人；1：主办人
        /*String zbrFlag = "0";
        String currentUserId = GetCurrentUserUtils.getCurrentUserId();
        if (sqsx != null && StringUtils.isNotBlank(sqsx.getFzbr())){
            if (sqsx.getFzbr().contains(currentUserId)){
                zbrFlag = "1";
            }
        }
        map.put("zbrFlag", zbrFlag);*/

        // 获取下环节待办人
        map.putAll(this.getNextNodeDbr(jobid,job));

        return map;
    }

    /**
     * 获取APP-获取当前流程任务相关信息
     */
    public Map getAppJobConcernByJobId(String jobid, SpcyJobXqVo job){
        Map map = new HashMap();

        if (job == null) {
            job = spcySqsxJobService.selectJobInfoById(jobid);
            if (job == null) {
                throw new BusinessException("当前流程任务不存在！");
            }
        }
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxInfoById(job.getFpid());

        map.put("sqsx", sqsx); // 申请事项基本信息
        map.put("job", job); // 当前流转记录
        return map;
    }

    /**
     * 获取下环节待办人
     * @param jobid
     * @param job
     * @return
     */
    private Map getNextNodeDbr(String jobid,SpcySqsxJob job){
        Map map = new HashMap();

        Map nextDbr = this.getNowJobInNextDbrAndHandlePageUrl(jobid, job);
        String nextNodeId = ""; // 下结点id
        String nextNode = "办结"; // 下结点名称
        if (nextDbr.get("nextNode") != null) {
            SysFlowNode nextFlowNode = (SysFlowNode) nextDbr.get("nextNode");
            nextNodeId = nextFlowNode.getId();
            nextNode = nextFlowNode.getFjdmc();
        }
        List<AgentVo> dbrListByMap = this.getDbrListByMap(nextDbr);

        map.put("nextNodeDbrList", dbrListByMap); // 下环节待办人list

        map.put("nextNode", nextNode); // 下流程结点名称
        map.put("nextNodeId", nextNodeId); // 下流程结点id

        return map;
    }

    /**
     * 获取下一个环节名称和待办人
     * @param job 流程任务
     * @param jobid 流程任务id
     * @return
     * {
     *     dbrId
     *     dbr:
     *     nextNode：下流程结点
     *     handlePageUrl：办理页面路径
     * }
     */
    private Map getNowJobInNextDbrAndHandlePageUrl(String jobid, SpcySqsxJob job){
        if (job == null && StringUtils.isNotBlank(jobid)){
            job = spcySqsxJobService.getById(jobid);
        }
        String flowCode = SpcyFlowConstant.KP_RCKH_FLOW_CODE;
        return flowNodeService.getNowJobInNextDbrAndHandlePageUrl(flowCode, job, null);
    }

    /**
     * 将待办人map转为List
     * @param map { dbrId: 待办人id ; dbr: 待办人 }
     */
    private List<AgentVo> getDbrListByMap(Map map){
        String dbrids = map.get("dbrId").toString(); // 待办人id
        String dbrs = map.get("dbr").toString(); // 待办人

        List<AgentVo> agentList = new ArrayList<>();

        if (StringUtils.isNotBlank(dbrids)) {
            String agentIds[] = dbrids.split(",|，");
            String agentNames[] = dbrs.split(",|，");

            for (int i = 0; i < agentIds.length; i++) {
                AgentVo agentVo = new AgentVo();
                agentVo.setDbrid(agentIds[i]);
                agentVo.setDbr(agentNames[i]);
                agentList.add(agentVo);
            }
        }

        return agentList;
    }

    /**
     * 获取当前环节待办人
     * @param pid 业务id
     * @param nodeName 环节名称
     * @return
     */
    public Map getNodeDbr(String pid, String nodeName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(pid);
        String dept = "";
        if (sqsx != null && StringUtils.isNotBlank(sqsx.getFywflcode())){
            dept = SpcyConstant.getYwclNameByCode(sqsx.getFywflcode());
        }

        if (nodeName.equals(SpcyFlowConstant.RCKP_HJMC_JBRSH)){
            // 主办人
            Map zbrMap = getZbrBySqsxId(sqsx, pid);
            dbrId = zbrMap.get("dbrId").toString();
            dbr = zbrMap.get("dbr").toString();
        }

        if (nodeName.equals(SpcyFlowConstant.RCKP_HJMC_KSFH)){
            // 科室负责人
            Map kzMap = getKsfzrByDeptName(dept);
            dbrId = kzMap.get("dbrId").toString();
            dbr = kzMap.get("dbr").toString();
        }

        if (nodeName.equals(SpcyFlowConstant.RCKP_HJMC_FGLDSP)){
            // 分管领导
            Map kzMap = getKsFgldByDeptName(dept);
            dbrId = kzMap.get("dbrId").toString();
            dbr = kzMap.get("dbr").toString();
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }


    // 获取主办人
    private Map getZbrBySqsxId(SpcySqsxBasic sqsx, String sqsxId){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        if (sqsx == null){
            SqsxZbrVo zbrVo = spcySqsxBaiscService.selectFzbrById(sqsxId);
            if (zbrVo != null && StringUtils.isNotBlank(zbrVo.getFzbr())){
                dbrId = zbrVo.getFzbr();

                if (StringUtils.isNotBlank(zbrVo.getFjbrmc())){
                    dbr = zbrVo.getFjbrmc();
                }else {
                    List<String> userIdList =  StringPatternUtils.getListByIds(zbrVo.getFzbr());
                    List<YcSysUser> userList = ycSysUserService.findRealnameListByIds(userIdList);
                    for (int i = 0; i<userList.size(); i++){
                        if (StringUtils.isBlank(dbr)) {
                            dbr = userList.get(i).getRealname();
                        } else {
                            dbr += "," + userList.get(i).getRealname();
                        }
                    }
                }
            }
        }else {
            List<String> userIdList =  StringPatternUtils.getListByIds(sqsx.getFzbr());
            List<YcSysUser> userList = ycSysUserService.findRealnameListByIds(userIdList);
            for (int i = 0; i<userList.size(); i++){
                if (StringUtils.isBlank(dbrId)) {
                    dbrId = userList.get(i).getId();
                    dbr = userList.get(i).getRealname();
                } else {
                    dbrId += "," + userList.get(i).getId();
                    dbr += "," + userList.get(i).getRealname();
                }
            }
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 对应科室的 科室负责人
    private Map getKsfzrByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName, SpcyConstant.GW_NAME_KZ);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId()+",";
                dbr += ksKz.getRealname()+",";
            }
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 获取对应科室的 分管领导
    private Map getKsFgldByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        YcSysUser fgldUserBydept = sysUserFgldService.getFgldUserBydept(deptName);
        if (fgldUserBydept != null) {
            dbrId = fgldUserBydept.getId();
            dbr = fgldUserBydept.getRealname();
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }
}

